package com.mac.livedatabus

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import com.mac.livedatabus.Constant.TEST_KEY
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main_2.*

class MainActivity : AppCompatActivity() {
    var count = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val observer = Observer<String> {
            Log.e("MainActivity", (++count).toString())
            mContentTv.append(it)
            mContentTv.append("\n")
        }
        mJump.setOnClickListener {
            startActivity(Intent(this, Main2Activity::class.java))
        }

        mJump3.setOnClickListener {
            startActivity(Intent(this, ThirdActivity::class.java))
        }

        mCancel.setOnClickListener {
            LiveDataBus.get<String>(TEST_KEY)
                .removeObserver(observer)
        }

        mButton.setOnClickListener {
            LiveDataBus.get<String>(Constant.TEST_KEY)
                .postValue("12132")
        }

        LiveDataBus.get<String>(TEST_KEY)
            .observe(this, Observer<String> {

            })

        LiveDataBus.get<String>(TEST_KEY)
            .observeStickyForever(observer)
    }
}
